from typing import List
import unittest

class Solution:
    def minOperations(self, nums: List[int], x: int) -> int:
        target = sum(nums) - x
        if target < 0:
            return -1
        left = 0
        right = -1
        current_sum = 0
        ret = 1000000
        while right < len(nums):
            if current_sum > target:
                current_sum -= nums[left]
                left += 1
            elif current_sum < target:
                if right + 1 < len(nums):
                    current_sum += nums[right + 1]
                right += 1
            if current_sum == target:
                ret = min(ret, len(nums) - (right - left + 1))
                if right + 1 < len(nums):
                    current_sum += nums[right + 1]
                right += 1

        return ret if ret != 1000000 else -1


class Test(unittest.TestCase):
    def test_case1(self):
        solution = Solution()
        target = solution.minOperations([3, 2, 20, 1, 1, 3], 10)
        self.assertEqual(target, 5)

    def test_case2(self):
        solution = Solution()
        target = solution.minOperations([5,2,3,1,1], 5)
        self.assertEqual(target, 1)

    def test_case3(self):
        solution = Solution()
        target = solution.minOperations([1,1], 3)
        self.assertEqual(target, -1)

    def test_case4(self):
        solution = Solution()
        target = solution.minOperations([8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309], 134365)
        self.assertEqual(target, 16)

    def test_case5(self):
        solution = Solution()
        target = solution.minOperations([5,1,4,2,3], 6)
        self.assertEqual(target, 2)
